package com.alan.thread;

import java.util.concurrent.*;

/**
 * 通过Runable和Thread， 无法获取子线程的运行结果。
 * Java5 引入了java.util.concurrent， 可以获取到子线程的运行结果。
 * Future接口可以理解成一个任务， Future.get()方法可以获取任务的运行结果
 * 例子步骤：
 * 1）创建一个RMTask和CUPDTask， 实现Callable接口。 重写call（）方法去实现任务
 * 2）通过Executors类去创建一个线程池
 * 3）通过Executors.submit()去创建子线程去执行任务。
 * Created by Alan on 17-10-9
 */
public class J5Thread {

    public static void main(String[] args) {
        J5Thread j5Thread = new J5Thread();
        // ES
        System.out.println("Get request from TC, thread id="+Thread.currentThread().getId());
        // 线程池 初始化十个线程 和JDBC连接池是一个意思 实现重用
        //ExecutorService ESP= Executors.newFixedThreadPool(10);
        ExecutorService ES= Executors.newCachedThreadPool();

        //task
        Callable<Integer> rmTask= j5Thread.new RMTask();
        Callable<Integer> cupdTask = j5Thread.new CUPDTask();

        //ES submit task,
        //param is callable
        //return result
        Future<Integer> rmResult=ES.submit(rmTask);
        Future<Integer> cupdResult= ES.submit(cupdTask);
        ES.shutdown();

        System.out.println("ES shutdown, thread id="+Thread.currentThread().getId());
        //ES shutdown

        try {
            Integer result = rmResult.get();
            System.out.println("process rmResult, thread id="+Thread.currentThread().getId());
            System.out.println("process rmResult="+result);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }

    //define task
    class RMTask implements Callable<Integer>{
        @Override
        public Integer call() throws Exception {
            System.out.println("creating response Rich media, thread id="+Thread.currentThread().getId());
            Thread.sleep(3000);
            System.out.println("end response Rich media, thread id="+Thread.currentThread().getId());
            return new Integer(3);
        }
    }

    class CUPDTask implements Callable<Integer> {
        @Override
        public Integer call() throws Exception {
            System.out.println("creating CUPD msg, thread id="+Thread.currentThread().getId());
            Thread.sleep(8000);
            System.out.println("end response Rich media, thread id="+Thread.currentThread().getId());
            return new Integer(10);
        }
    }

    public static void mainJava8(String[] args) {
        J5Thread j5Thread = new J5Thread();
        ExecutorService ES= Executors.newCachedThreadPool();
        Future<Integer> rmResult=ES.submit(()->{
            Thread.sleep(8000);
            return new Integer(10);
        });
        Future<Integer> cupdResult= ES.submit(()->{
            Thread.sleep(3000);
            return new Integer(10);
        });
        ES.shutdown();
        try {
            Integer result = rmResult.get();
            System.out.println("process rmResult="+result);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }



}
